//
//  SetView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI

//意见反馈
struct FeedbackView: View {
    @EnvironmentObject var pathManager: PathManager
    @EnvironmentObject var themeManager: ThemeManager
    @StateObject var viewModel = FeedbackViewModel()
    let bean : MsgBean?
    @State private var title: String = ""
    @State private var content: String = ""
    
    var body: some View {
        VStack(spacing: 15) {
            TextField("填写标题要突出您的重点", text: $title)
                .font(.system(size: 16))
                .foregroundColor(.bgItemTitle)
                .textFieldStyle(PlainTextFieldStyle())
                .clipShape(RoundedRectangle(cornerRadius: 8)) // 可选，用于保持圆角
                .padding()
                .background(.bgItem)
            TextField("请简单描述下您的意见，我们会第一时间处理", text: $content,axis: .vertical)
                .font(.system(size: 16))
                .textFieldStyle(PlainTextFieldStyle())
                .clipShape(RoundedRectangle(cornerRadius: 8)) // 可选，用于保持圆角
                .padding()
                .frame(minHeight: 100, maxHeight: 200, alignment: .topTrailing) // 设置最小和最大高度
                .background(.bgItem)
            if bean?.message?.isEmpty == false {
                HStack{
                    Text(bean?.message ?? "作者留言")
                        .font(.system(size: 16))
                        .foregroundStyle(.red)
                        .frame(minHeight: 50, alignment: .topTrailing) // 设置最小和最大高度
                    Spacer(minLength: 0)
                    
                }.padding()
                    .background(.bgItem)
            }
            if bean?.jump ?? 0 != 0 && !isExamine() {
                Button(action: {
                    viewModel.jump(type: bean?.jump ?? 0)
                }) {
                    Text(bean?.btnName ?? "确定")
                        .foregroundColor(themeManager.themeColor)
                        .font(.system(size: 16))
                        .padding(.horizontal,10)
                        .padding(.vertical,15)
                        .frame(width: 300)
                    
                }.background(themeManager.themeColor.opacity(0.1))
                    .cornerRadius(8)
            }
            if bean == nil {
                Button(action: {
                    //执行保存
                    viewModel.publish(title: title, content: content)
                }) {
                    Text("提交")
                        .foregroundColor(themeManager.themeColor)
                        .font(.system(size: 16))
                        .padding(.horizontal,10)
                        .padding(.vertical,15)
                        .frame(width: 300)
                    
                }.background(themeManager.themeColor.opacity(0.1))
                    .cornerRadius(8)
            }
            Spacer()
        }
        .background(.bg)
        .navigationTitle("意见反馈")
        .toolbar {
            if bean == nil {
                // 添加一个右标题按钮
                ToolbarItem(placement: .navigationBarTrailing) {
                    Text("我的意见")
                        .foregroundColor(themeManager.themeColor)
                        .font(.system(size: 12))
                        .padding(.horizontal,10)
                        .padding(.vertical,7)
                        .cornerRadius(8)
                        .contentShape(Rectangle()).onTapGesture {
                            pathManager.jump(type: JumpType.MY_FEEDBACK)
                        }
                }
            }
        }
        .onAppear{
            title = bean?.title ?? ""
            content = bean?.content ?? ""
            
        }
        //监听事件变化
        .onReceive(viewModel.$isSuc) { value in
            if value {
                pathManager.back()
            }
        }
        
    }
}

//#Preview {
//    SetView()
//}


//定义预览提供者
struct Feedback_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: FeedbackView(bean : nil))
    }
}
